/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/

package SelfBookingSystem.MyFramework;

import java.io.IOException;
import java.net.URL;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.data.contacts.ContactGroupFeed;
import com.google.gdata.data.contacts.GroupMembershipInfo;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.FullName;
import com.google.gdata.data.extensions.Name;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class ContactAPI {
	
	private ContactEntry contact = new ContactEntry();
	private ContactGroupEntry group = new ContactGroupEntry();
    final String NO_YOMI = null;
    private ContactsService myService = new ContactsService("exampleCo-exampleApp-1");
	//Konstruktor setzt Instanzenvariablen auf Defaultwert
    public ContactAPI(){
    	
    	//Empty Constructor
    }

	
    public int GetConnection(String ConfigValueEmail, String ConfigValuePassword){
    	int result=0;
    	try {
    		myService.setUserCredentials(ConfigValueEmail, ConfigValuePassword);
    	} catch (AuthenticationException e) {
    		result=9999;
    	}
    	return result;
    }
    
    
    public void setFullName(String FirstName, String LastName){
    	Name name = new Name();
    	name.setFullName(new FullName(FirstName + " " + LastName, NO_YOMI));
		contact.setName(name);
    }
    
    public void setPrimaryEmail(String Email){
	    Email primaryMail = new Email();
	    primaryMail.setAddress(Email);
	    primaryMail.setRel("http://schemas.google.com/g/2005#home");
	    primaryMail.setPrimary(true);
	    contact.addEmailAddress(primaryMail);
    }
    
    public String getGroupID(String GroupName){
    	ContactGroupFeed resultFeed = new ContactGroupFeed();
    	String MyContactGroupID = new String();
    	try {
			URL feedUrl = new URL("http://www.google.com/m8/feeds/groups/default/full");
	        resultFeed = myService.getFeed(feedUrl, ContactGroupFeed.class);
	        // Print the results
	        System.out.println(resultFeed.getTitle().getPlainText());
    	}
		 catch (IOException e) {	
				e.printStackTrace();
		} catch (ServiceException e) {
				e.printStackTrace();
		} 

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            ContactGroupEntry groupEntry = resultFeed.getEntries().get(i);
        	if(groupEntry.getTitle().getPlainText().equals(GroupName)){
        		MyContactGroupID = groupEntry.getId();
                System.out.println("ContactAPI - Id: " + groupEntry.getId());
                System.out.println("ContactAPI - Group Name: " + groupEntry.getTitle().getPlainText());
        	}
        }

    	return  MyContactGroupID;
    }
    
    public void AddUserToGroup(String GroupName){
        
    	String MyContactGroupID = getGroupID(GroupName);
    	
        GroupMembershipInfo gm = new GroupMembershipInfo();
        gm.setHref(MyContactGroupID);
        contact.addGroupMembershipInfo(gm);
   }
    
    public void setPrimaryPhoneNumber(String PhoneNumber){
    	PhoneNumber myPhoneNumber = new PhoneNumber();
    	myPhoneNumber.setPhoneNumber(PhoneNumber);
    	myPhoneNumber.setRel("http://schemas.google.com/g/2005#home");
    	myPhoneNumber.setPrimary(true);
    	contact.addPhoneNumber(myPhoneNumber);
    }
    
    public void CreateGroup(String GroupName){
    	group.setTitle(new PlainTextConstruct(GroupName));
    	//group.addExtendedProperty(additionalInfo);
    }    
    
    public String InsertContact(){
    	String tmp = new String();
		try {
			URL postUrl = new URL("http://www.google.com/m8/feeds/contacts/default/full");
			ContactEntry result = myService.insert(postUrl, contact);
			System.out.println("ContactAPI - ID: " + result.getId());
			
			tmp = result.getId();
		}
		 catch (IOException e) {	
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		} 

    	return tmp;
    }
    public String InsertGroup(){
    	String tmp = new String();
		try {
			URL postUrl = new URL("http://www.google.com/m8/feeds/groups/default/full");
			ContactGroupEntry result = myService.insert(postUrl, group);
			System.out.println("ContactAPI - ID: " + result.getId());
			tmp = result.getId();
		}
		 catch (IOException e) {	
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		} 

    	return tmp;
    }

        
}
